#pragma once

#include "../libs/httplib.h"
#include "../libs/easylog.h"
#include "../controllers/purchasecontroller.hpp"
#include "../util.hpp"
#include "../result.hpp"

namespace pharmacy_server
{
    struct PurchaseBusiness
    {
        static void create_purchase_order(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("createPurchaseOrder body: {}", req.body);

                Json::Value root = json_util::deserialize(req.body);
                auto medicine_name = root["medicine_name"].asString();
                auto supplier = root["supplier"].asString();
                auto quantity = root["quantity"].asInt();
                auto purchase_date = root["purchase_date"].asString();

                Result result = PurchaseController::instance().create_purchase_order(medicine_name, supplier, quantity, purchase_date);
                rsp.set_content(result.serialize(), "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("createPurchaseOrder business error: {}", e.what());
            }
        }

        static void query_purchase_records(const httplib::Request& req, httplib::Response& rsp)
        {
            try
            {
                easylog::debug("queryPurchaseRecords body: {}", req.body);

                Json::Value root = json_util::deserialize(req.body);
                auto medicine_name = root["medicine_name"].asString();
                auto supplier = root["supplier"].asString();
                auto purchase_date = root["purchase_date"].asString();

                Result result = PurchaseController::instance().query_purchase_records(medicine_name, supplier, purchase_date);
                rsp.set_content(result.serialize(), "application/json");
            }
            catch (const std::exception& e)
            {
                easylog::error("queryPurchaseRecords business error: {}", e.what());
            }
        }
    };
}